{

An atomicity test on V.  This also demonstrates reading and writing to a globally shared variable "V".

}

CON

  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000

obj

''-[ object declarations!!!!!! ]-
  d     :       "Parallax Serial Terminal"
  
var

  long V, Vc
  long Stack[32]
 
pub Reader

  d.start(115200)

  waitcnt(cnt + clkfreq<<2)     'time to get to the serial terminal and enable it, 4 seconds
  
  cognew(Writer, @stack)
  
  d.str(string("main started", 13))
  
  repeat                        'repeat forever
  
    Vc := V                     'Vc is a copy of V, read V here!
    
                                'you do not need to copy V, but be careful because V is always being changed by the Writer method running in another cog.
                                'This means that you could get different values of V than you might expect at a given time.

 
    'Since we copy V into a value that doesnt change during the loop iteration, we will never get to the Fail state 

    if ((Vc <> 0) and (Vc <> -1))  'Check to see if any bits in Vc are out of place, accesses the variable directly
      Fail
    
  
pri Writer

  d.str(string("writer started", 13))
 
  repeat                        'repeat forever
  
     V := 0                     'set all bits in V to 0
     V := -1                    'set all bits in V to 1
    


'Since we copy V into a value that doesnt change during the loop iteration, we will never get to the Fail state 
pri Fail

  d.str(string("FAILED!!!", 13))
  d.dec(Vc)
  d.char(13)